import java.io.IOException;

public class TestScanner {

	private static ScannerTokenManager tm;

	private static SimpleCharStream instream;

	public static void main(String args[]) {
		Token t;
		Object value;

		java.io.InputStream infile;
		boolean loop = true;
		if (args.length < 1) {
			infile = System.in;
		} else
			try {
				infile = new java.io.FileInputStream(args[0]);
			} catch (java.io.FileNotFoundException e) {
				System.out.println("File " + args[0] + " not found.");
				return;
			}

		instream = new SimpleCharStream(infile);
		tm = new ScannerTokenManager(instream);
		t = nextTokenSkipErrors();

		while (t.kind != ScannerConstants.EOF) {
			System.out.print("String scanned: [" + t.image
					+ "]\n\t->Token kind: ");
			System.out.print(ScannerConstants.tokenImage[t.kind].toUpperCase());
			value = t.getValue();
			System.out.println(";\tToken value: "
					+ (value instanceof String ? "\"" + value + "\""
							: (value instanceof Character ? "'" + value + "'"
									: value)));
			t = nextTokenSkipErrors();
		}
	}

	private static Token nextTokenSkipErrors() {
		Token t = null;
		try {
			t = tm.getNextToken();
			
		} catch (TokenMgrError e) {
			System.out.println(e.toString());
			try {
				instream.readChar();
				
			} catch (IOException e1) {
				// don't care
			}
			tm.ReInit(instream);
			return nextTokenSkipErrors();
		}
		return t;
	}
}
